
> # set up environment ----
> library(data.table)

> library(ggplot2)

> library(Hmisc)

> library(gridExtra)

> source("R/00-functions.R")

> load("data/analysis_data.RData")

> # type2: Cox PH ranking
> respondent_candidate_data[, status := 1]

> model1 <- coxph(
+   Surv(ranking, status) ~ preferences + strata(subject_id),
+   data = respondent_candidate_data,
+   id = subject_id)

> model2 <- coxph(
+   Surv(ranking, status) ~ electability + strata(subject_id),
+   data = respondent_candidate_data,
+   id = subject_id)

> model3 <- coxph(
+   Surv(ranking, status) ~ expected_utility + strata(subject_id),
+   data = respondent_candidate_data,
+   id = subject_id)

> # type3 = Cox PH reverse ranking
> model1a <- coxph(
+   Surv(8-ranking, status) ~ preferences + strata(subject_id),
+   data = respondent_candidate_data,
+   id = subject_id)

> model2a <- coxph(
+   Surv(8-ranking, status) ~ electability + strata(subject_id),
+   data = respondent_candidate_data,
+   id = subject_id)

> model3a <- coxph(
+   Surv(8-ranking, status) ~ expected_utility + strata(subject_id),
+   data = respondent_candidate_data,
+   id = subject_id)

> # which direction should we expect to be better? type 3 has lowest AICs
> mapply(`-`,
+   lapply(list(model1, model2, model3), AIC),
+   lapply(list(model1a, model2a, model3a), AIC))
[1] 446.2865 313.4015 252.0959

> residuals <- cbind(model1a$residuals, model2a$residuals, model3a$residuals)

> respondent_types_a <- do.call(rbind, lapply(1:nrow(respondent_data), function(x) {
+   y <- residuals[((x - 1) * 7 + 1):(x * 7), ]
+   SSR <- colSums(y ^ 2)
+   exp(-SSR) / sum(exp(-SSR))
+   #which.min(SSR)
+ }))

> # type2: Cox PH ranking
> respondent_candidate_data <- respondent_candidate_data[n_attention_fails >= 1 &
+     trump_least_preferred == 1 &
+     support_2016 != "Trump"]

> model1 <- coxph(
+   Surv(ranking, status) ~ preferences + strata(subject_id),
+   data = respondent_candidate_data,
+   id = subject_id)

> model2 <- coxph(
+   Surv(ranking, status) ~ electability + strata(subject_id),
+   data = respondent_candidate_data,
+   id = subject_id)

> model3 <- coxph(
+   Surv(ranking, status) ~ expected_utility + strata(subject_id),
+   data = respondent_candidate_data,
+   id = subject_id)

> # type3 = Cox PH reverse ranking
> model1a <- coxph(
+   Surv(8-ranking, status) ~ preferences + strata(subject_id),
+   data = respondent_candidate_data,
+   id = subject_id)

> model2a <- coxph(
+   Surv(8-ranking, status) ~ electability + strata(subject_id),
+   data = respondent_candidate_data,
+   id = subject_id)

> model3a <- coxph(
+   Surv(8-ranking, status) ~ expected_utility + strata(subject_id),
+   data = respondent_candidate_data,
+   id = subject_id)

> # which direction should we expect to be better? type 3 has lowest AICs
> mapply(`-`,
+   lapply(list(model1, model2, model3), AIC),
+   lapply(list(model1a, model2a, model3a), AIC))
[1] 254.0824 105.0075 174.3281

> respondent_data[, chisq.test(table(type2, type3))]

	Pearson's Chi-squared test

data:  table(type2, type3)
X-squared = 279.84, df = 4, p-value < 2.2e-16


> respondent_data[, main := as.numeric(
+   n_attention_fails >= 1 &
+     trump_least_preferred == 1 &
+     support_2016 != "Trump"
+ )]

> respondent_data[main == 0 & order %like% "^E", .(
+   mean(type3 == "preferences"),
+   mean(type3 == "electability"),
+   mean(type3 == "expected_utility")
+ )]
          V1        V2        V3
       <num>     <num>     <num>
1: 0.3591331 0.3003096 0.3405573

> respondent_data[main == 0 & order %like% "^P", .(
+   mean(type3 == "preferences"),
+   mean(type3 == "electability"),
+   mean(type3 == "expected_utility")
+ )]
          V1        V2       V3
       <num>     <num>    <num>
1: 0.4239766 0.2046784 0.371345

> exp(-colSums(head(residuals, 7) ^ 2)) / sum(exp(-colSums(head(residuals, 7) ^ 2)))
[1] 0.56245894 0.08132058 0.35622048
